package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (服务消费方)预约设置管理
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference//dubbo远程调用
    private OrderSettingService orderSettingService;

    //文件上传（Excel）
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //读入excel文件，解析
            List<String[]> list = POIUtils.readExcel(excelFile);
            //list封装成OrderSetting对象
            List<OrderSetting> data = new ArrayList<>();
            //确保读取到的列表不为空且包含数据
            if(list != null && list.size() > 0){
                for (String[] strings : list) {
                    //strings[0]是日期
                    String date = strings[0];
                    System.out.println("date是Excel文件中每一行的第一格的数据："
                            +date);
                    //strings[1]是可预约人数
                    String number = strings[1];
                    System.out.println("number是Excel文件中每一行的第二格的数据"
                            +number);
                    OrderSetting orderSetting = new OrderSetting
                            (new SimpleDateFormat("yyyy/MM/dd").parse(date),//格式化日期
                                    Integer.parseInt(number));//可预约数量
                    data.add(orderSetting);
                }
            }
            //data是List集合，批量导入
            orderSettingService.add(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);//ORDERSETTING_SUCCESS
        } catch (Exception e) {
            //文章解析失败
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);//ORDERSETTING_FAIL
        }
    }

    //根据年月查询对应的预约设置信息，翻日历页时也会调用该方法
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){//2024-7
        try{
            //用OrderSetting并不合理，因为OrderSetting的属性(比如orderDate)是不符合前端的要求1的
            //前端数据需要{ date: 1, number: 120, reservations: 1 }，
            // 而OrderSetting的属性orderDate是date类型，Map里的key就可以自由控制，
            // 可以封装一个包含三个属性(date(日期的号数),number,reservations)的orderSettingData作为他的Object
            //然后传回给前端
            List<Map<String,Object>> list = orderSettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //根据日期修改可预约人数
    @RequestMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.editNumberByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
